package com.dunqi.gpm.shunqing.home.fragment;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.base.RecyclerAdapter;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.common.view.RecyclerItemDecoration;
import com.dunqi.gpm.shunqing.home.activity.KnotCommentActivity;
import com.dunqi.gpm.shunqing.home.bean.KnotComment;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;
import com.dunqi.gpm.shunqing.home.widget.NormalView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/12/2.
 */

public class KnotCommentFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;

    List<KnotComment> data = new ArrayList<>();
    private KnotCommentAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryOwnedTaskList(page)
                .compose(RxSchedulers.<Page<KnotComment>>io_main())
                .subscribe(new Consumer<Page<KnotComment>>() {
                    @Override
                    public void accept(Page<KnotComment> pageData) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);

                        ArrayList<KnotComment> list = pageData.getList();

                        if (page == 1) {
                            data.clear();
                        }

                        data.addAll(list);

                        page++;

                        adapter.notifyDataSetChanged();
                        if (pageData.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd(true);
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        SwipeUtil.init(swipeRefresh);

        recycler.setLayoutManager(new LinearLayoutManager(context));

        adapter = new KnotCommentAdapter(data);
        recycler.setAdapter(adapter);
        recycler.addItemDecoration(new RecyclerItemDecoration(1, RecyclerItemDecoration.VERTICAL));
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, recycler);
        swipeRefresh.setOnRefreshListener(this);
        adapter.setEmptyView(new EmptyView(context));
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        KnotComment knotComment = data.get(position);
        KnotCommentActivity.start(getActivity(), knotComment);
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    class KnotCommentAdapter extends RecyclerAdapter<KnotComment> {

        public KnotCommentAdapter(@Nullable List<KnotComment> data) {
            super(R.layout.item_knot_comment, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, KnotComment item) {

            NormalView project_name_view = helper.getView(R.id.project_name_view);
            String projectName = item.getProjectName();
            project_name_view.setText(projectName);

            NormalView agent_type_view = helper.getView(R.id.agent_type_view);
            String agent_type = item.getTodoTaskTypeText();
            agent_type_view.setText(agent_type);

            NormalView industry_categories_view = helper.getView(R.id.industry_categories_view);
            String industry_categories = item.getFirstIndustryText();
            industry_categories_view.setText(industry_categories);

            NormalView responsible_unit_view = helper.getView(R.id.responsible_unit_view);
            String responsible_unit = item.getTodoTaskDesc();
            responsible_unit_view.setText(responsible_unit);

            NormalView main_leader_view = helper.getView(R.id.main_leader_view);
            String main_leader = item.getCreateTime();
            main_leader_view.setText(main_leader);

            TextView commentTv = helper.getView(R.id.comment_tv);
            commentTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_knot_comment;
    }


}
